package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.support.Parameter;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 获取所有检查组列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<CheckGroup> checkGroupList = checkGroupService.findAll();
            return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.err.println(queryPageBean);
        try {
            PageResult pageResult = checkGroupService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            System.err.println(pageResult);
            return pageResult;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new PageResult(0L,null);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        System.err.println(checkGroup);
        try {
            checkGroupService.add(checkGroup,checkItemIds);
            return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConst.ADD_CHECKITEM_FAIL);
    }

    @RequestMapping("/getById")
    public Result getById( Integer id){
        System.err.println("******************"+id);
        try {
            CheckGroup checkGroup = checkGroupService.getById(id);
            System.err.println(checkGroup);
            return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConst.ACTION_FAIL);
    }



}
